package com.brush.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brush.common.exception.database.PrimaryKeyException;
import com.brush.common.exception.database.RecordNotFoundException;
import com.brush.system.domain.SysOperLog;
import com.brush.system.domain.req.sysoperlog.SysOperLogReq;
import com.brush.system.domain.resp.sysoperlog.SysOperLogResp;
import com.brush.system.mapper.SysOperLogMapper;
import com.brush.system.service.SysOperLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 18494
 * @description 针对表【sys_oper_log(操作日志记录)】的数据库操作Service实现
 * @createDate 2024-08-19 10:28:03
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
        implements SysOperLogService {

    @Override
    public PageInfo<SysOperLogResp> paginationQuery(SysOperLogReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            // 调用sysConfigMapper[mybatis]的分页查询方法, 要么使用lambdaQueryWrapper来写【mybatis-plus】
            // 一定返回的结果是 List<SysConfigResp>
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public SysOperLogResp findById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryKeyException();
        }

        // 根据主键查询
        SysOperLog db = this.getById(id);
        // 数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        // 封装成返回对象
        SysOperLogResp resp = new SysOperLogResp();
        BeanUtils.copyProperties(db, resp);

        return resp;
    }
}




